#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "log.h"

using namespace LOG;

int main()
{
    // 创建套接字
    int sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        lg(FATAL, "socket create fail...");
        return -1;
    }
    lg(DEBUG, "socket create success,sockfd: %d", sockfd);
    // 绑定套接字和网络信息
    struct sockaddr_in local;
    memset(&local, 0, sizeof local); // local 置0
    local.sin_family = AF_INET;//用于网络通信
    local.sin_port = htons(8888);//端口号
    local.sin_addr.s_addr = INADDR_ANY;//IP地址：接收任意ip
    int m = ::bind(sockfd,(struct sockaddr*)&local ,sizeof local);
    if(m == -1)
    {
        lg(FATAL,"socket bind fail...");
        return -2;
    }
    lg(DEBUG,"socket bind success...");

    //接收client消息——阻塞接收
    char inbuffer[1024];
    struct sockaddr_in src_addr;
    socklen_t len;
    ssize_t s = recvfrom(sockfd,(void*)inbuffer,sizeof inbuffer,0,
    (struct sockaddr*)&src_addr,&len);

    if(s < 0)
    {
        //lg(FATAL,"recvfrom fail...");
        return -3;
    }
    printf("Client say: %s\n",inbuffer);
    close(sockfd);
    return 0;
}
